package cn.hrlly.tao2shou.biz;

import android.content.Context;

import com.zhy.http.okhttp.OkHttpUtils;

import cn.hrlly.tao2shou.other.Config;
import cn.hrlly.tao2shou.callback.LoginCallback;
import cn.hrlly.tao2shou.domain.User;
import cn.hrlly.tao2shou.listener.OnLoginListener;
import cn.hrlly.tao2shou.utils.DatabaseUtil;

/**
 * 联网获取登录状态
 * User: gengliang
 * Date: 2016-01-22
 * Time: 07:56
 */

public class LoginBizImpl implements LoginBiz {

    private OnLoginListener onLoginListener;
    private DatabaseUtil databaseUtil;

    public LoginBizImpl(OnLoginListener onLoginListener,Context context){
        this.onLoginListener=onLoginListener;
        this.databaseUtil=new DatabaseUtil(context);
    }

    @Override
    public void login(User user) {
        OkHttpUtils
                .post()
                .url(Config.key_url)
                .addParams(Config.key_action,Config.action_login)
                .addParams(Config.key_phone_number, user.getPhoneNumber())
                .addParams(Config.key_password,user.getPassword())
                .addParams(Config.key_longditude, String.valueOf(user.getLongditude()))
                .addParams(Config.key_lantitude, String.valueOf(user.getLantitude()))
                .build()
                .execute(new LoginCallback(onLoginListener) );

    }

    @Override
    public void saveUserInfo(User user) {
        databaseUtil.insertUserInfo(user);
    }

    @Override
    public void closeDataBase() {
        databaseUtil.closeDatabase();
    }
}
